﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH
{
    public partial class MyDuiZhangdan : ClientBasePage
    {
        private ExcuteDao dao = new ExcuteDao();
        private decimal total1 = 0;
        private decimal total2 = 0;
        private decimal total3 = 0;
        private decimal total4 = 0;
        private decimal total5 = 0;
        private decimal total6 = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[SessionKeys.CURRENT_MEMBER] == null)
            {
                Response.Redirect("index.aspx", true);
                return;
            }
            if (!IsPostBack)
            {
                ViewState["InformationAll"] = dao.Get_Customer_GetMyMoney(
                    ((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).M_Name);

                //初次加载清空分页状态
                //if (string.IsNullOrEmpty(Request.QueryString["pageIndex"]) && Session["UrlType"] == null)
                //{
                //    Session["SortExpression_BuyList"] = null;
                //    Session["SortDirection_BuyList"] = null;
                //}
                SearchInformationBindData();
            }
        }
        private void setSessionData()
        {
            //检索条件记录

            if (Session["BuyListIndex"] != null)
            {
                gvInformation.PageIndex = (int)Session["BuyListIndex"];
            }
        }
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            SearchInformationBindData();
        }
        #region 信息列表事件

        /// <summary>
        /// 信息列表数据加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInformation_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("id", e.Row.RowIndex.ToString());
                e.Row.Attributes.Add("onKeyDown", "SelectRow();");
                e.Row.Attributes.Add("onClick", "MarkRow(" + e.Row.RowIndex.ToString() + ");");

                //((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "../Customer/EditBuy.aspx?InformationID="
                //    + SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[0].Controls[0]).Text).Replace("&", "amp;");

                //e.Row.Cells[1].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);

                //string jianjie = SpecialCharHandler.HtmlDiscode(e.Row.Cells[2].Text);
                //e.Row.Cells[2].ToolTip = jianjie;
                //if (jianjie.Length > 15)
                //{
                //    jianjie = jianjie.Substring(0, 15) + "...";
                //}
                //e.Row.Cells[2].Text = jianjie;
                //e.Row.Cells[3].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[3].Text);
                //e.Row.Cells[4].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[4].Text);
                e.Row.Cells[2].Text =  Convert.ToDecimal(e.Row.Cells[2].Text).ToString("0.00");
                e.Row.Cells[3].Text = Convert.ToDecimal(e.Row.Cells[3].Text).ToString("0.00");
                e.Row.Cells[4].Text = Convert.ToDecimal(e.Row.Cells[4].Text).ToString("0.00");
                e.Row.Cells[5].Text = Convert.ToDecimal(e.Row.Cells[5].Text).ToString("0.00");
                
                e.Row.Cells[7].Text = Convert.ToDecimal(e.Row.Cells[7].Text).ToString("0.00");

                total1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CUSCP_CSSGFE"));
                total2 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CUSCP_CSSGJE"));
                total3 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CUSCP_CSCB"));
                total4 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CUSCP_ZXJG"));
                total5 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CUSCP_SYFE"));
                total6 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CUSCP_CPSZ"));

            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "当前页合计";
                e.Row.Cells[1].Text = ""; //(total1 + total2 + total3 + total4 + total6).ToString("0.00");
                e.Row.Cells[2].Text = total1.ToString("0.00");
                e.Row.Cells[3].Text = total2.ToString("0.00");
                e.Row.Cells[4].Text = total3.ToString("0.00");
                e.Row.Cells[5].Text = total4.ToString("0.00");
                e.Row.Cells[6].Text = total5.ToString();
                e.Row.Cells[7].Text = total6.ToString("0.00");
                e.Row.Font.Bold = true;         
            }

        }
        #endregion

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState.Clear();
            ViewState["InformationAll"] = dao.Get_Customer_GetMyMoney(
                    ((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).M_Name);
            //清空分页状态
            Session["SortExpression_BuyList"] = null;
            Session["SortDirection_BuyList"] = null;

            SearchInformationBindData();

        }

        /// <summary>
        /// 选择页面功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInformation_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvInformation.PageIndex = e.NewPageIndex;
            pageIndex = e.NewPageIndex;
            Session["BuyListIndex"] = pageIndex;
            SearchInformationBindData();
        }

        /// <summary>
        /// GridView排序事件
        /// </summary>
        protected void gvInformation_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string SortExpression_BuyList = e.SortExpression.ToString();
            //// 假定为排序方向为“顺序”
            string SortDirection_BuyList = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (SortExpression_BuyList == this.gvInformation.Attributes["SortExpression_BuyList"])
            {
                //获得下一次的排序状态
                SortDirection_BuyList = (this.gvInformation.Attributes["SortDirection_BuyList"].ToString() == SortDirection_BuyList ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.gvInformation.Attributes["SortExpression_BuyList"] = SortExpression_BuyList;
            this.gvInformation.Attributes["SortDirection_BuyList"] = SortDirection_BuyList;
            //记录分页状态
            Session["SortExpression_BuyList"] = SortExpression_BuyList;
            Session["SortDirection_BuyList"] = SortDirection_BuyList;

            setSortColumnStyle(this.gvInformation, SortExpression_BuyList, SortDirection_BuyList);
            this.SearchInformationBindData();
        }

        /// <summary>
        /// 绑定到GridView
        /// </summary>
        private void SearchInformationBindData()
        {
            // 获取GridView排序数据列及排序方向
            string SortExpression_BuyList = this.gvInformation.Attributes["SortExpression_BuyList"];
            string SortDirection_BuyList = this.gvInformation.Attributes["SortDirection_BuyList"];
            // 调用业务数据获取方法
            DataTable dtBind = (DataTable)ViewState["InformationAll"];

            //页面数据总数
            recordCount = dtBind.Rows.Count;

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(SortExpression_BuyList)) && (!string.IsNullOrEmpty(SortDirection_BuyList)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", SortExpression_BuyList, SortDirection_BuyList);
            }
            AspNetPager1.PageSize = 10;
            //初始化分页数据源实例            
            PagedDataSource pds = new PagedDataSource();
            //设置总行数           
            AspNetPager1.RecordCount = dtBind.Rows.Count;
            //设置分页的数据源          
            pds.DataSource = dtBind.DefaultView;
            //设置当前页         
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            //设置每页显示页数           
            pds.PageSize = AspNetPager1.PageSize;
            //启用分页         
            pds.AllowPaging = true;
            //设置GridView的数据源为分页数据源          
            gvInformation.DataSource = pds;
            // GridView绑定并显示数据
            // this.gvInformation.DataSource = dtBind;
            this.gvInformation.DataBind();

            pageCount = gvInformation.PageCount;
            pageIndex = gvInformation.PageIndex;

        }
    }
}